| 1 |  |  | 'use strict'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | var express	 	= require('express'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | var router 		= express.Router(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | var passport 	= require('passport'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | var User = require('../models/user'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | var Room = require('../models/room'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | // Home page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | router.get('/', function(req, res, next) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	// If user is already logged in, then redirect to rooms page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	if(req.isAuthenticated()){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 		res.redirect('/rooms'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	else{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		res.render('login', { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 			success: req.flash('success')[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 			errors: req.flash('error'),  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 			showRegisterForm: req.flash('showRegisterForm')[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | // Login | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | router.post('/login', passport.authenticate('local', {  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	successRedirect: '/rooms',  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	failureRedirect: '/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	failureFlash: true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | // Register via username and password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | router.post('/register', function(req, res, next) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	var credentials = {'username': req.body.username, 'password': req.body.password }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	if(credentials.username === '' || credentials.password === ''){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		req.flash('error', 'Missing credentials'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		req.flash('showRegisterForm', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		res.redirect('/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	}else{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		// Check if the username already exists for non-social account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		User.findOne({'username': new RegExp('^' + req.body.username + '$', 'i'), 'socialId': null}, function(err, user){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 			if(err) throw err; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			if(user){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 				req.flash('error', 'Username already exists.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 				req.flash('showRegisterForm', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 				res.redirect('/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			}else{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 				User.create(credentials, function(err, newUser){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 					if(err) throw err; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 					req.flash('success', 'Your account has been created. Please log in.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 					res.redirect('/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 				}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | // Social Authentication routes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | // 1. Login via Facebook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | router.get('/auth/facebook', passport.authenticate('facebook')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | router.get('/auth/facebook/callback', passport.authenticate('facebook', { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		successRedirect: '/rooms', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		failureRedirect: '/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		failureFlash: true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | // 2. Login via Twitter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | router.get('/auth/twitter', passport.authenticate('twitter')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | router.get('/auth/twitter/callback', passport.authenticate('twitter', { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		successRedirect: '/rooms', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		failureRedirect: '/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		failureFlash: true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | // Rooms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | router.get('/rooms', [User.isAuthenticated, function(req, res, next) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	Room.find(function(err, rooms){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		if(err) throw err; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		res.render('rooms', { rooms }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | }]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | // Chat Room  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | router.get('/chat/:id', [User.isAuthenticated, function(req, res, next) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	var roomId = req.params.id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	Room.findById(roomId, function(err, room){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		if(err) throw err; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		if(!room){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			return next();  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		res.render('chatroom', { user: req.user, room: room }); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | }]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  | // Logout | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  | router.get('/logout', function(req, res, next) { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 	// remove the req.user property and clear the login session | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 	req.logout(); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  | 	 | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  | 	// since req.logout() may not work, use req.session.destroy() instead | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  | 	req.session.destroy(function (err) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 		if (err) { return next(err); } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 		// destroy session data | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 		req.session = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 		// redirect to homepage | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 		res.redirect('/'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 	}); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  | module.exports = router; | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  | 
            
                        
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.